package com.ckm.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ckm.entity.College;
import com.ckm.entity.Major;
import com.ckm.exception.BusinessException;
import com.ckm.mapper.CollegeMapper;
import com.ckm.mapper.MajorMapper;
import com.ckm.service.MajorService;
import com.ckm.util.FindObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class MajorServiceImpl implements MajorService {

    @Autowired
    private MajorMapper majorMapper;
    @Autowired
    private CollegeMapper collegeMapper;

    @Override
    public void addMajor(List<Major> majorList) {
        if (majorList == null || majorList.isEmpty()) {
            throw new BusinessException(250,"添加数据集为空");
        }
        for (Major major : majorList) {
            if (!findMajor(major).isEmpty()) {
                throw new BusinessException(250,"数据重复:"+major);
            }
            if (collegeMapper.selectById(major.getColId())==null){
                throw new BusinessException(250,"无法找到学院："+major.getColId());
            }
            majorMapper.insert(major);
        }
        log.info("添加数据：{}", majorList);
    }

    @Override
    public void deleteMajor(Integer id) {
        if (id==null){
            throw new BusinessException(250,"删除参数为空");
        }
        if (majorMapper.selectById(id)==null){
            throw new BusinessException(250,"无此专业");
        }
        majorMapper.deleteById(id);
    }

    @Override
    public void updateMajor(Major major) {
        if (major.getId()==null){
            throw new BusinessException(250,"更新信息缺少id");
        }
        Major select = majorMapper.selectById(major.getId());
        if (select==null){
            throw new BusinessException(250,"无此专业");
        }
        if (select.equals(major)){
            throw new BusinessException(250,"信息无更改");
        }
        majorMapper.updateById(major);
    }

    @Override
    public PageInfo<Major> getMajorPage(Major major, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(findMajor(major));
    }

    @Override
    public List<Major> findMajor(Major major) {
        QueryWrapper<Major> wrapper = new QueryWrapper<>();
        FindObjectUtil.fullQueryWrapper(major,wrapper);
        return majorMapper.selectList(wrapper);
    }

    @Override
    public JSONArray statistics(String colName) {
        QueryWrapper<College> collegeQueryWrapper = new QueryWrapper<>();
        if (colName!=null && !colName.isEmpty()){
            collegeQueryWrapper.eq("col_name",colName);
        }
        List<College> collegeList = collegeMapper.selectList(collegeQueryWrapper);
        if (collegeList==null || collegeList.isEmpty()){
            throw new BusinessException(250,"无此学院");
        }
        QueryWrapper<Major> majorQueryWrapper = new QueryWrapper<>();
        if (collegeList.size()==1){
            majorQueryWrapper.eq("col_id",collegeList.getFirst().getId());
        }
        List<Major> majorList = majorMapper.selectList(majorQueryWrapper);
        Map<String, List<Major>> collect = majorList.stream().collect(Collectors.groupingBy(Major::getColId));
        ArrayList<Map<String,Object>> result = new ArrayList<>();
        collegeList.forEach(v->{
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("colName",v.getColName());
            collect.forEach((k1,v1)->{
                List<Map<String, Object>> majorLists = new ArrayList<>();
                if (k1.equals(v.getId().toString())){
                    v1.forEach(v2->{
                        Map<String, Object> majors = new LinkedHashMap<>();
                        majors.put("majorId",v2.getId());
                        majors.put("majorName",v2.getMajorName());
                        majorLists.add(majors);
                    });
                    map.put("majorList",majorLists);
                }
            });
            result.add(map);
        });
        JSONArray jsonArray = new JSONArray(result);
        log.info("统计结果:{}",jsonArray);
        return jsonArray;
    }
}
